home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 December / 2004-12 CHIP.iso / Narzedzia systemowe / Inno Setup 5.0.4 Beta / isetup-5.0.4-beta.exe / {app} / Examples / CodeAutomation.iss next >
Text File  |  2004-08-02  |  8KB  |  273 lines

  1. ; -- CodeAutomation.iss --
  2. ;
  3. ; This script shows how to use the COM Automation object support.
  4.  
  5. [Setup]
  6. AppName=My Program
  7. AppVerName=My Program version 1.5
  8. CreateAppDir=no
  9. DisableProgramGroupPage=yes
  10. DefaultGroupName=My Program
  11. UninstallDisplayIcon={app}\MyProg.exe
  12.  
  13. [Code]
  14.  
  15. {--- SQLDMO ---}
  16.  
  17. const
  18.   SQLServerName = 'localhost';
  19.   SQLDMOGrowth_MB = 0;
  20.  
  21. procedure SQLDMOButtonOnClick(Sender: TObject);
  22. var
  23.   SQLServer, Database, DBFile, LogFile: Variant;
  24.   IDColumn, NameColumn, Table: Variant;
  25. begin
  26.   if MsgBox('Setup will now connect to Microsoft SQL Server ''' + SQLServerName + ''' via a trusted connection and create a database. Do you want to continue?', mbInformation, mb_YesNo) = idNo then
  27.     Exit;
  28.  
  29.   { Create the main SQLDMO COM Automation object }
  30.  
  31.   try
  32.     SQLServer := CreateOleObject('SQLDMO.SQLServer');
  33.   except
  34.     RaiseException(ExceptionType, 'Please install Microsoft SQL server connectivity tools first.'#13#13'(Error '''+ExceptionParam+''' occured)');
  35.   end;
  36.  
  37.   { Connect to the Microsoft SQL Server }
  38.  
  39.   SQLServer.LoginSecure := True;
  40.   SQLServer.Connect(SQLServerName);
  41.   
  42.   MsgBox('Connected to Microsoft SQL Server ''' + SQLServerName + '''.', mbInformation, mb_Ok);
  43.  
  44.   { Setup a database }
  45.  
  46.   Database := CreateOleObject('SQLDMO.Database');
  47.   Database.Name := 'Inno Setup';
  48.   
  49.   DBFile := CreateOleObject('SQLDMO.DBFile');
  50.   DBFile.Name := 'ISData1';
  51.   DBFile.PhysicalName := 'c:\program files\microsoft sql server\mssql\data\IS.mdf';
  52.   DBFile.PrimaryFile := True;
  53.   DBFile.FileGrowthType := SQLDMOGrowth_MB;
  54.   DBFile.FileGrowth := 1;
  55.  
  56.   Database.FileGroups.Item('PRIMARY').DBFiles.Add(DBFile);
  57.  
  58.   LogFile := CreateOleObject('SQLDMO.LogFile');
  59.   LogFile.Name := 'ISLog1';
  60.   LogFile.PhysicalName := 'c:\program files\microsoft sql server\mssql\data\IS.ldf';
  61.  
  62.   Database.TransactionLog.LogFiles.Add(LogFile);
  63.   
  64.   { Add the database }
  65.  
  66.   SQLServer.Databases.Add(Database);
  67.  
  68.   MsgBox('Added database ''' + Database.Name + '''.', mbInformation, mb_Ok);
  69.  
  70.   { Setup some columns }
  71.  
  72.   IDColumn := CreateOleObject('SQLDMO.Column');
  73.   IDColumn.Name := 'id';
  74.   IDColumn.Datatype := 'int';
  75.   IDColumn.Identity := True;
  76.   IDColumn.IdentityIncrement := 1;
  77.   IDColumn.IdentitySeed := 1;
  78.   IDColumn.AllowNulls := False;
  79.  
  80.   NameColumn := CreateOleObject('SQLDMO.Column');
  81.   NameColumn.Name := 'name';
  82.   NameColumn.Datatype := 'varchar';
  83.   NameColumn.Length := '64';
  84.   NameColumn.AllowNulls := False;
  85.   
  86.   { Setup a table }
  87.  
  88.   Table := CreateOleObject('SQLDMO.Table');
  89.   Table.Name := 'authors';
  90.   Table.FileGroup := 'PRIMARY';
  91.   
  92.   { Add the columns and the table }
  93.   
  94.   Table.Columns.Add(IDColumn);
  95.   Table.Columns.Add(NameColumn);
  96.  
  97.   Database.Tables.Add(Table);
  98.  
  99.   MsgBox('Added table ''' + Table.Name + '''.', mbInformation, mb_Ok);
  100. end;
  101.  
  102. {--- IIS ---}
  103.  
  104. const
  105.   IISServerName = 'localhost';
  106.   IISServerNumber = '1';
  107.   IISURL = 'http://127.0.0.1';
  108.  
  109. procedure IISButtonOnClick(Sender: TObject);
  110. var
  111.   IIS, WebSite, WebServer, WebRoot, VDir: Variant;
  112.   ErrorCode: Integer;
  113. begin
  114.   if MsgBox('Setup will now connect to Microsoft IIS Server ''' + IISServerName + ''' and create a virtual directory. Do you want to continue?', mbInformation, mb_YesNo) = idNo then
  115.     Exit;
  116.  
  117.   { Create the main IIS COM Automation object }
  118.  
  119.   try
  120.     IIS := CreateOleObject('IISNamespace');
  121.   except
  122.     RaiseException(ExceptionType, 'Please install Microsoft IIS first.'#13#13'(Error '''+ExceptionParam+''' occured)');
  123.   end;
  124.  
  125.   { Connect to the IIS server }
  126.  
  127.   WebSite := IIS.GetObject('IIsWebService', IISServerName + '/w3svc');
  128.   WebServer := WebSite.GetObject('IIsWebServer', IISServerNumber);
  129.   WebRoot := WebServer.GetObject('IIsWebVirtualDir', 'Root');
  130.  
  131.   { (Re)create a virtual dir }
  132.  
  133.   try
  134.     WebRoot.Delete('IIsWebVirtualDir', 'innosetup');
  135.     WebRoot.SetInfo();
  136.   except
  137.   end;
  138.  
  139.   VDir := WebRoot.Create('IIsWebVirtualDir', 'innosetup');
  140.   VDir.AccessRead := True;
  141.   VDir.AppFriendlyName := 'Inno Setup';
  142.   VDir.Path := 'C:\inetpub\innosetup';
  143.   VDir.AppCreate(True);
  144.   VDir.SetInfo();
  145.  
  146.   MsgBox('Created virtual directory ''' + VDir.Path + '''.', mbInformation, mb_Ok);
  147.  
  148.   { Write some html and display it }
  149.  
  150.   if MsgBox('Setup will now write some HTML and display the virtual directory. Do you want to continue?', mbInformation, mb_YesNo) = idNo then
  151.     Exit;
  152.  
  153.   ForceDirectories(VDir.Path);
  154.   SaveStringToFile(VDir.Path + '/index.htm', '<html><body>Inno Setup rocks!</body></html>', False);
  155.   if not ShellExec('open', IISURL + '/innosetup/index.htm', '', '', SW_SHOWNORMAL, ewNoWait, ErrorCode) then
  156.     MsgBox('Can''t display the created virtual directory: ''' + SysErrorMessage(ErrorCode) + '''.', mbError, mb_Ok);
  157. end;
  158.  
  159. {--- MSXML ---}
  160.  
  161. const
  162.   XMLURL = 'http://cvs.jrsoftware.org/view/*checkout*/ishelp/isxfunc.xml';
  163.   XMLFileName = 'isxfunc.xml';
  164.   XMLFileName2 = 'isxfuncmodified.xml';
  165.  
  166. procedure MSXMLButtonOnClick(Sender: TObject);
  167. var
  168.   XMLHTTP, XMLDoc, NewNode, RootNode: Variant;
  169.   Path: String;
  170. begin
  171.   if MsgBox('Setup will now use MSXML to download XML file ''' + XMLURL + ''' and save it to disk.'#13#13'Setup will then load, modify and save this XML file. Do you want to continue?', mbInformation, mb_YesNo) = idNo then
  172.     Exit;
  173.     
  174.   { Create the main MSXML COM Automation object }
  175.  
  176.   try
  177.     XMLHTTP := CreateOleObject('MSXML2.ServerXMLHTTP');
  178.   except
  179.     RaiseException(ExceptionType, 'Please install MSXML first.'#13#13'(Error '''+ExceptionParam+''' occured)');
  180.   end;
  181.   
  182.   { Download the XML file }
  183.  
  184.   XMLHTTP.Open('GET', XMLURL, False);
  185.   XMLHTTP.Send();
  186.  
  187.   Path := ExpandConstant('{src}\');
  188.   XMLHTTP.responseXML.Save(Path + XMLFileName);
  189.  
  190.   MsgBox('Downloaded the XML file and saved it as ''' + XMLFileName + '''.', mbInformation, mb_Ok);
  191.  
  192.   { Load the XML File }
  193.  
  194.   XMLDoc := CreateOleObject('MSXML2.DOMDocument');
  195.   XMLDoc.async := False;
  196.   XMLDoc.resolveExternals := False;
  197.   XMLDoc.load(Path + XMLFileName);
  198.   if XMLDoc.parseError.errorCode <> 0 then
  199.     RaiseException(erCustomError, 'Error on line ' + IntToStr(XMLDoc.parseError.line) + ', position ' + IntToStr(XMLDoc.parseError.linepos) + ': ' + XMLDoc.parseError.reason);
  200.   
  201.   MsgBox('Loaded the XML file.', mbInformation, mb_Ok);
  202.  
  203.   { Modify the XML document }
  204.   
  205.   NewNode := XMLDoc.createElement('isxdemo');
  206.   RootNode := XMLDoc.documentElement;
  207.   RootNode.appendChild(NewNode);
  208.   RootNode.lastChild.text := 'Hello, World';
  209.  
  210.   { Save the XML document }
  211.  
  212.   XMLDoc.Save(Path + XMLFileName2);
  213.  
  214.   MsgBox('Saved the modified XML as ''' + XMLFileName2 + '''.', mbInformation, mb_Ok);
  215. end;
  216.  
  217.  
  218. {--- Word ---}
  219.  
  220. procedure WordButtonOnClick(Sender: TObject);
  221. var
  222.   Word: Variant;
  223. begin
  224.   if MsgBox('Setup will now check whether Microsoft Word is running. Do you want to continue?', mbInformation, mb_YesNo) = idNo then
  225.     Exit;
  226.  
  227.   { Try to get an active Word COM Automation object }
  228.   
  229.   try
  230.     Word := GetActiveOleObject('Word.Application');
  231.   except
  232.   end;
  233.   
  234.   if VarIsEmpty(Word) then
  235.     MsgBox('Microsoft Word is not running.', mbInformation, mb_Ok)
  236.   else
  237.     MsgBox('Microsoft Word is running.', mbInformation, mb_Ok)
  238. end;
  239.  
  240. {---}
  241.  
  242. procedure CreateButton(ALeft, ATop: Integer; ACaption: String; ANotifyEvent: TNotifyEvent);
  243. begin
  244.   with TButton.Create(WizardForm) do begin
  245.     Left := ALeft;
  246.     Top := ATop;
  247.     Width := WizardForm.CancelButton.Width;
  248.     Height := WizardForm.CancelButton.Height;
  249.     Caption := ACaption;
  250.     OnClick := ANotifyEvent;
  251.     Parent := WizardForm;
  252.   end;
  253. end;
  254.  
  255. procedure InitializeWizard();
  256. var
  257.   Left, Top, TopInc: Integer;
  258. begin
  259.   Left := WizardForm.WelcomeLabel2.Left;
  260.   TopInc := WizardForm.CancelButton.Height + 8;
  261.   Top := WizardForm.WelcomeLabel2.Top + WizardForm.WelcomeLabel2.Height - 4*TopInc;
  262.  
  263.   CreateButton(Left, Top, '&SQLDMO...', @SQLDMOButtonOnClick);
  264.   Top := Top + TopInc;
  265.   CreateButton(Left, Top, '&IIS...', @IISButtonOnClick);
  266.   Top := Top + TopInc;
  267.   CreateButton(Left, Top, '&MSXML...', @MSXMLButtonOnClick);
  268.   Top := Top + TopInc;
  269.   CreateButton(Left, Top, '&Word...', @WordButtonOnClick);
  270. end;
  271.  
  272.  
  273.